﻿define(function(require, exports, module) {

    var axios = require('lib/axios/index.js');
    var mCard = require('components/card/index.js');

    var layer = require('components/layer/index.js');
    var _ = require("lib/lodash.js");

    var VueComponent = new Vue({
        el: '#app',
        data: {
            articleId: 0,
            articleView: {
                Title: '',
                SubTitle: '',
                DataFlag: '',
                IsDel: '',
                Summary: '',
                CategoryId:0
            },
            categoryArr: [],
            selectedOptions: [],
            selectedOptions2: []
        },
        mounted: function() {

            var me = this;
            var tools = require('lib/tools.js');

            var map = tools.getUrlParamsMap();
            this.articleId = map.Id;
            //alert(this.articleId);

            //var thisVue = this;
            ////thisVue.dataListSearch();

            this.articleViewSearch();
            //this.dataListSearch();

            //先写死吧
            //this.bindSelect();

        },
        methods: {
            handleChange(value) {
                console.log(value);
            },
            bindSelect: function () {
                var thisVue = this;
                var url = baseSiteUrl + "Category/Tree";
                axios.get(url)
                    .then(function (response) {


                        _.forEach(response.data.ResultList, function (item) {
                            item.value = item.Id;
                            item.label = item.Name;
                            item.children = _.children(item.Children, function (two) {
                                two.value = two.Id;
                                two.label = two.Name;
                                two.children = two.Children;
                            })
                        });

                        thisVue.categoryArr = response.data.ResultList;

                      
                    });

            },
            onSubmit() {
                console.log(this.articleView);
                var me = this;
                var url = baseSiteUrl + "Article/SaveOrUpdate";

            
                $.ajax({
                    type: 'POST',
                    url: url,
                    dataType: 'json ',
                    data: this.articleView,
                    success: function (res) {
                        toastr.success('操作成功');
                        window.location.href = "ArticleBasicEdit.html?Id="+res.Id;
                    }
                })
            },
            uploadImgUrl: function (i, num) {
                var me = this;
                layer.alert('小主,让您久等了,功能开发中...');
                return false;
                //开始上传 回调后 赋值

                if (num == 1) {
                    me.dataList[i]["ImageUrlOne"] = "";
                } else {
                    me.dataList[i]["ImageUrlTwo"] = "";
                }
            },
            clearImgUrl: function (i, num) {
                var me = this;
                if (num == 1) {
                    me.dataList[i]["ImageUrlOne"] = "";
                } else {
                    me.dataList[i]["ImageUrlTwo"] = "";
                }
            },
            inputImgUrl: function (i, num) {
                var me = this;
                layer.prompt({ title: '随便写点啥，并确认', formType: 2 }, function (text, index) {
                    layer.close(index);

                    if (num == 1)
                    {
                        me.dataList[i]["ImageUrlOne"] = text;
                    }else{
                        me.dataList[i]["ImageUrlTwo"] = text;
                    }
                    

                });
            },
            articleViewSearch: function() {

                var thisVue = this;

                var url = baseSiteUrl + "Article/GetById/" + this.articleId;

                axios.get(url)
                    .then(function (response) {
                        thisVue.articleView = response.data;
                        thisVue.articleView.DataFlag = thisVue.articleView.DataFlag.toString();
                        thisVue.articleView.IsDel = thisVue.articleView.IsDel.toString();
                        thisVue.articleView.CategoryId = thisVue.articleView.CategoryId.toString();

                    });
            },
            dataListSearch: function() {
                var me = this;

                var url = baseSiteUrl + "Common/Query?query_key=ArticleContentView&ArticleId=" + this.articleId;

                axios.get(url)
                    .then(function(response) {
                        me.dataList = response.data.ResultList;

                      
                        Vue.nextTick(function () {

                            for (var index = 0; index < me.dataList.length; index++) {
                                var editor = KindEditor.create('#editor_' + index, {
                                    allowFileManager: true,
                                    afterChange: function () {
                                        this.sync();
                                        var index = $(this.srcElement).attr("id").slice(7);
                                        me.dataList[index].Content = $(this.srcElement).val();

                                    }
                                });
                            }
                        });



                    });
            },
            bindUpload: function() {
                uploader = WebUploader.create({
                    // 选完文件后，是否自动上传。
                    auto: true,

                    // swf文件路径
                    swf: applicationPath + 'web/src/lib/webuploader/Uploader.swf',

                    // 文件接收服务端。
                    server: '../../Tools/UpLoadProcess',

                    // 选择文件的按钮。可选。
                    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                    pick: '#filePicker',

                    //只允许选择图片
                    accept: {
                        title: 'Images',
                        extensions: 'gif,jpg,jpeg,bmp,png',
                        mimeTypes: 'image/jpg,image/jpeg,image/png' //修改这行
                    }
                });
            },

            saveOrUpdate: function() {
                var me = this;
                var url = baseSiteUrl + "Article/SaveOrUpdateDTO";

                var obj = {
                    'Article': JSON.stringify(me.articleView),
                    "ArticleContentList": JSON.stringify(me.dataList)
                }


                $.ajax({
                    type: 'POST',
                    url: url,
                    dataType: 'json ',
                    data: obj,
                    success: function(res) {
                        toastr.success('操作成功');

                        me.articleViewSearch();
                        me.dataListSearch();
                    }
                })
            },
            add: function() {
                var me = this;
                var new_default = {
                    "Id": 0,
                    "ArticleId": 1,
                    "Content": "",
                    "ImageUrlOne": "",
                    "ImageUrlTwo": "",
                    "ImageType": 2,
                    "DataFlag": 1,
                    "IsDel": 0
                }
                me.dataList.push(new_default);

                Vue.nextTick(function () {
                    var index = me.dataList.length - 1;

                    var editor = KindEditor.create('#editor' + index, {
                        allowFileManager: true,
                        afterChange: function () {
                            this.sync();
                            var index = $(this.srcElement).attr("id").slice(7);
                            me.dataList[index].Content = $(this.srcElement).val();

                        }
                    });
                });

            }

        }

    });

});